﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Threading;  
using System.Threading.Tasks;
using System.Collections.Concurrent;
namespace test1
{
    class SingletonClass
    {
        private SingletonClass()
        {

        }
        private static readonly SingletonClass instance = new SingletonClass();

        internal static SingletonClass Instance
        {
            get { return SingletonClass.instance; }
        } 


    }
    class Program
    {
        static List<string> files = new List<string>();
        static FileSystemWatcher fsw;
        const int waitTime = 20000;
        static void Main(string[] args)
        {
            SingletonClass only = SingletonClass.Instance;

            string path = @"C:\file";
            fsw = new FileSystemWatcher(path);
            fsw.Created += new FileSystemEventHandler(fsw_Created);
            fsw.EnableRaisingEvents = true;
            Thread ProcessThread = new Thread(Process);
            ProcessThread.Start();
            //Console.ReadLine();
        }

        static void fsw_Created(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine(e.FullPath);
            FileInfo f = new FileInfo(e.FullPath);
            long len = f.Length;
            Thread.Sleep((int)(len / waitTime));
            //f.Refresh();
            //while (len<f.Length)
            //{
            //    Thread.Sleep(1000);
            //    len = f.Length;
            //    f.Refresh();
            //}
            
            lock (files)
            {
                files.Add(e.FullPath);
            }

        }

        static void fsw_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine(e.FullPath);
            Console.WriteLine(GetHashString(e.FullPath));
        }
        static void Listen()
        {
            WaitForChangedResult wcr;
            while (true)
            {
                wcr = fsw.WaitForChanged(WatcherChangeTypes.Created);
                files.Add(fsw.Path + "\\" + wcr.Name);
            }
        }
        static void Process()
        {
            //string file;
            while (true)
            {
                Thread.Sleep(2000);
                while (files.Count != 0)
                {


                    lock (files)
                    {
                        try
                        {
                            Parallel.ForEach(files, item =>
                            {

                                Console.WriteLine(item + GetHashString(item));
                                
                                files.Remove(item);
                            });

                        }
                        catch (System.Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                    }

                }
            }
        }
        static string GetHashString(string filePath)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hash;
            Thread.SpinWait(100);
            using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                hash = md5.ComputeHash(fs);
                return BitConverter.ToString(hash);
            }
        }
    }
}
